//
//  YZLMyCollectionViewController.m
//  Car
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLMyCollectionViewController.h"
#import "YZLCollectionModel.h"
#import "YZLCarCollectListCell.h"
#import "YZLCarDetailViewController.h"

@interface YZLMyCollectionViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation YZLMyCollectionViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - data
- (void)dataHandle {
    
    self.arr = [NSMutableArray array];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"收藏"];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [path stringByAppendingPathComponent:@"Car.plist"];
    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
//    NSLog(@"%@", self.arr);
    if (self.arr.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"亲! 您还没有收藏! 请快去收藏吧!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark - alert 协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的收藏";
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    
    UIView *view = [[UIView alloc] init];
    self.tableView.tableFooterView = view;
    
    [self dataHandle];
    
    //返回
    [self skipReturnWithTarget:self];
    
    //bar颜色
    [self navigationbarColor];
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLCollectionModel *model = self.arr[indexPath.row];
    YZLCarCollectListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[YZLCarCollectListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.model = model;
    return cell;
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YZLCarDetailViewController *vc = [[YZLCarDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    YZLCollectionModel *model = self.arr[indexPath.row];
    vc.seriesId = model.nID;
}

#pragma mark - 添加编辑方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //文件夹路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建文件夹
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"Car.plist"];
    
    //通过editingStyle判断状态,执行删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        /* 将本地的文件取出放入数组中 实现本地删除 */
        NSMutableArray *deleteArr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:strPath]];
        [deleteArr removeObjectAtIndex:indexPath.row];
        //        NSLog(@"%@", self.arr);
        //删除cell之前 先从数组中对应位置 删除数据
        [self.arr removeObjectAtIndex:indexPath.row];
        //删除的信息需要被存放在一个数组中
        [NSKeyedArchiver archiveRootObject:deleteArr toFile:strPath];
        //cell删除
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
